﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using YouHP.Utility;
using System.Drawing.Drawing2D;

namespace YouHP.BLL.MedicalGraph
{
    public class GraphGeneral : GraphBase
    {
        public List<Brush> mRangeBrush;
        List<GraphStyle.VariableStyle> mVarStyles;


        public GraphGeneral()
        {
            mLegendName = "";
            mRangeBrush = new List<Brush>();

            mRangeBrush.Add(new SolidBrush(Color.FromArgb(153, 204, 255)));
            mRangeBrush.Add(new SolidBrush(Color.White));
            mRangeBrush.Add(new SolidBrush(Color.FromArgb(192, 192, 192)));
            mRangeBrush.Add(new SolidBrush(Color.FromArgb(254, 204, 153)));


            mVarStyles = new List<GraphStyle.VariableStyle>();
            GraphStyle.VariableStyle style = new GraphStyle.VariableStyle();
            style.SetColor(Color.Red);
            mVarStyles.Add(style);
            style = new GraphStyle.VariableStyle();
            style.SetColor(Color.FromArgb(255, 153, 0));
            mVarStyles.Add(style);
            style = new GraphStyle.VariableStyle();
            style.SetColor(Color.FromArgb(51, 153, 102));
            mVarStyles.Add(style);
        }

        #region 取得数据在图形上的位置

        protected override double GetDataY(int index, DateTime date)
        {
            return GetDataY(index, 0);
        }
        protected override double GetDataY(int index, double y)
        {
            int cnt = Rule.GetVariableCount();
            double per = (index + Rule.GetVariable(index).GetRelPosition(y)) / cnt;
            return _DataRect.Bottom - _DataRect.Height * per;
        }

        protected override void SetupAxis()
        {
            return;
        }

        protected override void DrawData()
        {
            int x1;
            int y1;
            int i, j;
            int cnt2, cnt1;
            Rectangle rect1;

            //count = Data.Count;
            cnt1 = Rule.GetVariableCount();
            int lasty = -1;
            int lastx = -1;
            StringFormat drawformat = new StringFormat();
            drawformat.Alignment = StringAlignment.Center;
            drawformat.LineAlignment = StringAlignment.Center;
            Pen pen = new Pen(Color.Black, 1.0f);
            Font font = new Font("宋体", 9.0f, FontStyle.Bold);

            i = 0;
            for(i=0; i < cnt1; i++)
            {
                GraphRule.Variable varitem = Rule.GetVariable(i);
                GraphStyle.VariableStyle style = mVarStyles[i];
                GraphDataVariableInfo varinfo = Data.GetDataVariable(varitem.mKey);
                cnt2 = varinfo.DataItemList.Count;
                lasty = -1;
                for (j = 0; j < cnt2; j++)
                {
                    GraphDataItemInfo iteminfo = varinfo.DataItemList[j];
                    x1 = (int)this.GetDataX(0, iteminfo.DateTime1);
                    double dd = Utils.StrToDouble(iteminfo.Value, -1000);
                    if (dd <= -999)
                    {
                        lasty = -1;
                        continue;
                    }
                    y1 = (int)this.GetDataY(j, dd);
                    rect1 = new Rectangle(x1 - 2, y1 - 2, 4, 4);
                    _GraphicsData.DrawRectangle(style.mPen, (float)(x1 - 1.5), (float)(y1 - 1.5), 3f, 3f);
                    if (lasty > 0)
                    {
                        _GraphicsData.DrawLine(style.mPen, lastx, lasty, x1, y1);
                    }
                    lasty = y1;
                    lastx = x1;
                }

                i++;
            }
        }

        protected override void DrawDataGrid()
        {
            int x1, x2;
            int y1, y2;
            int count;
            int i;

            GraphRule.Variable var = Rule.GetVariable(0);
            Rectangle rect = new Rectangle(0, 0, Rule.DataWidth, Rule.Height);

            count = var.mUpper.Count;
            for (i = 0; i < count; i++)
            {
                y1 = (int)GetDataY(0, var.mUpper[i]);
                y2 = (int)GetDataY(0, var.mLower[i]);
                x1 = (int)GetDataX(0, 0);
                x2 = (int)GetDataX(0, _GridCount);
                _GraphicsData.FillRectangle(mRangeBrush[i], x1, y1, x2 - x1, y2 - y1);
            }

            x1 = rect.Left;
            x2 = rect.Right - 10;
            count = Rule.GetVariableCount();
            for (i = 0; i < count; i++)
            {
                y1 = (int)this.GetDataY(i, 0.0);
                _GraphicsData.DrawLine(Rule.mXAxisPen, x1, y1, x2, y1);
            }


            y1 = this._DataRect.Top;
            y2 = this._DataRect.Bottom;
            for (i = 1; i <= _GridCount; i++)
            {
                x1 = (int)this.GetDataX(0, i);
                _GraphicsData.DrawLine(Rule.mGridPen, x1, y1, x1, y2);
                _GraphicsData.DrawString(i.ToString(),
                    Rule.mGridFont, Brushes.Black, x1, y2, Rule.mLabelFotmat);
            }

        }

        protected override void SetupPage()
        {
            /*
            _GridCount = Param.mGridCount > Data.Count ? Param.mGridCount : Data.Count;
            Param.mDataWidth = _GridCount * 30 + 30;
            //mParam.mLegendWidth = mParam.mVariables.Count * 50 + 200;
            _DataRect = new Rectangle(0, 30, Param.mDataWidth, Param.mHeight - 60);
            */
        }

        protected override void DrawLegend()
        {

            int i;
            int x1, x2;
            int y1;
            Pen pen1;
            Font font1;
            Rectangle rect1;
            int cnt1;
            StringFormat dformat;

            GraphRule.Variable var;
            Graphics g = this.mGraphicsLegend;
            dformat = new StringFormat();
            dformat.Alignment = StringAlignment.Far;
            dformat.LineAlignment = StringAlignment.Far;
            font1 = new Font("宋体", 9.0f);
            x1 = 40;
            x2 = 150;
            cnt1 = Rule.GetVariableCount();
            pen1 = new Pen(Color.Black, 1.0f);
            y1 = 30;
            g.DrawRectangle(pen1, x1 - 10, y1, 125, cnt1 * 40 + 10);
            y1 += 10;
            g.DrawString(mLegendName, font1, Brushes.Black, x1, y1);
            y1 += 20;
            for (i = 0; i < cnt1; i++)
            {
                var = Rule.GetVariable(cnt1 - 1 - i);
                g.DrawString(var.mName, font1, Brushes.Black, x2, y1, dformat);
                y1 += 15;
                rect1 = new Rectangle(x1, y1, 50, 15);
                g.DrawString(var.Unit, font1, Brushes.Black, x2, y1, dformat);
                y1 += 25;
            }

            var = Rule.GetVariable(0);
            cnt1 = var.mLower.Count;
            g.DrawRectangle(pen1, x1 - 10, y1 - 8, 125, cnt1 * 20 + 10);
            for (i = 0; i < cnt1; i++)
            {

                rect1 = new Rectangle(x1, y1, 50, 15);
                g.FillRectangle(mRangeBrush[cnt1 - 1 - i], rect1);
                g.DrawRectangle(pen1, rect1);
                g.DrawString(var.mRangeName[cnt1 - 1 - i], font1, Brushes.Black, x1 + 90, y1 + 17, dformat);
                y1 += 20;

            }
        }

        protected override void DrawLegendGrid()
        {
            int i, j;
            int x1;
            int y1, y2;
            Pen pen1;
            Font font1;
            int cnt1, cnt2;
            StringFormat dformat;
            double val;

            // 画出区域
            GraphRule.Variable var = Rule.GetVariable(0);
            cnt1 = var.mUpper.Count;
            for (i = 0; i < cnt1; i++)
            {
                y1 = (int)GetDataY(0, var.mUpper[i]);
                y2 = (int)GetDataY(0, var.mLower[i]);
                this.mGraphicsLegend.FillRectangle(mRangeBrush[i], Rule.LegendWidth - 5, y1, _DataRect.Right, y2 - y1);
            }

            int left = Rule.LegendWidth - 5;
            x1 = left;
            //GraphRule.Variable var = mParam.mVariables[0];
            y1 = (int)GetDataY(0, var.mUpper[0]);
            y2 = (int)GetDataY(0, var.mLower[0]);
            //this.mGraphicsLegend.FillRectangle(var.mRangBrush[0], x1, y1, mDataRect.Right, y2 - y1);
            pen1 = new Pen(Color.Black, 2.0f);
            mGraphicsLegend.DrawLine(pen1, x1, y2, _DataRect.Right, y2);
            font1 = new Font("宋体", 9.0f);
            cnt1 = Rule.GetVariableCount();
            dformat = new StringFormat();
            dformat.Alignment = StringAlignment.Far;
            dformat.LineAlignment = StringAlignment.Center;
            for (i = 0; i < cnt1; i++)
            {
                var = Rule.GetVariable(i);
                GraphStyle.VariableStyle style = mVarStyles[i];
                x1 = left;
                y1 = (int)(_DataRect.Bottom - i * 1.0 / cnt1 * _DataRect.Height);
                y2 = (int)(_DataRect.Bottom - (i + 1.0) * 1.0 / cnt1 * _DataRect.Height);
                mGraphicsLegend.DrawLine(pen1, x1, y1, _DataRect.Right, y1);
                //mGraphicsLegend.DrawLine(pen1, x1, y1, x1, y2);
                cnt2 = var.GridValue.Count - 1;
                for (j = 0; j < cnt2; j++)
                {
                    val = var.GridValue[j];
                    y1 = (int)GetDataY(i, val);
                    mGraphicsLegend.DrawLine(style.mPen, x1, y1, x1 + 5, y1);
                    mGraphicsLegend.DrawString(val.ToString(), font1, Brushes.Black, x1 - 4, y1, dformat);
                }
                y1 = (int)GetDataY(i, var.GridValue[cnt2]);
                mGraphicsLegend.DrawString(var.ShortName, font1, Brushes.Black, x1 - 2, y1, dformat);

            }

            left = Rule.LegendWidth - 5;
            x1 = left;
            var = Rule.GetVariable(0);


            y2 = _DataRect.Bottom;
            pen1 = new Pen(Color.Black, 2.0f);
            mGraphicsLegend.DrawLine(pen1, x1, y2, _DataRect.Right, y2);
            pen1.CustomEndCap = new AdjustableArrowCap(4, 6);
            mGraphicsLegend.DrawLine(pen1, x1, y2, x1, _DataRect.Top);
        }

        #endregion
    }
}
